﻿using RpcCentral.Collect;
using RpcCentral.Collect.Controller;
using RpcCentral.Service.Interface;
using RpcModel.Server;

namespace RpcCentral.Service.Service
{
    internal class HeartbeatService : IHeartbeatService
    {
        private IRpcServerCollect _Server;
        private ISyncRunStateCollect _RunState;
        private ISyncSignalStateCollect _SignalState;
        public HeartbeatService(IRpcServerCollect server,
            ISyncSignalStateCollect signalState,
            ISyncRunStateCollect runState)
        {
            this._SignalState = signalState;
            this._Server = server;
            this._RunState = runState;
        }

        public void Heartbeat(ServiceHeartbeat obj, string conIp)
        {
            RpcServerController server = _Server.GetRpcServer(obj.ServerId);
            if (server.ServerOnline(conIp))
            {
                _RunState.Sync(obj.RunState, obj.ServerId);
                _SignalState.Sync(obj.ServerId, obj.RemoteState);
            }
        }
    }
}
